﻿namespace WriterStars.Data.Repository
{
    using System.Linq;
    using WriterStars.Model;

    public class UserRepository : IUserRepository
    {
        private IWriterStarsDbContext db;

        public UserRepository()
        {
            this.db = new WriterStarsDbContext();
        }

        public UserRepository(IWriterStarsDbContext context)
        {
            this.db = context;
        }

        public User GetById(string id)
        {
            return this.db.Users.Find(id);
        }


        

        public bool Update(User user)
        {
            var original = db.Users.Find(user.Id);
            int changes = 0;
            if (original != null)
            {
                db.Entry(original).CurrentValues.SetValues(user);
                changes = db.SaveChanges();
            }

            return changes != 0;
        }


        public IQueryable<User> GetAll()
        {
            return this.db.Users;
        }
    }
}
